package com.zhuanzhuan.hero.user.interceptor;

import com.zhuanzhuan.hero.user.util.EncryptDecryptUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @ClassName ParamListFieldEncryptInterceptor
 * @Description 加密拦截器
 *  与com.zhuanzhuan.hero.user.interceptor.EncryptInterceptor互补，!!!仅处理list类型的字段
 * @Author hanjunjun
 * @Date 2024/7/22 17:00
 */
@Slf4j
@Aspect
@Component
public class ParamListFieldEncryptInterceptor {

    @Around(value = "@annotation(com.zhuanzhuan.hero.user.annotations.EncryptedMethod)")
    public Object handler(ProceedingJoinPoint joinPoint) {
        //入参取注解标识的形参   对注解标注的字段加密
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        Method method = signature.getMethod();
        String methodName = method.getName();
        log.info("ParamListFieldEncryptInterceptor method:{}", methodName);

        //入参加密
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            try {
                //处理list类型的   isStringType传false
                EncryptDecryptUtil.objectEncrypt(arg, false);
            } catch (IllegalAccessException e) {
                log.error("objectEncrypt异常", e);
            }
        }

        try {
            return joinPoint.proceed();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }
}